<?php
//if(isset($_GET['scores_by_term']))
//{
//	$_SESSION['whichS'] = 'view';
//}

if(isset($_SESSION['whichS']) and $_SESSION['whichS'] == 'change')
{

}

if(isset($_SESSION['whichS']) and $_SESSION['whichS'] == 'view')
{
	if(isset($_SESSION['view_type']) and $_SESSION['view_type'] == 'all')
	{
		$student_id = $_SESSION['student_id'];

		// list all subjects' scores for specified student_id
		$sql = "SELECT a.mmh, ten_mh, phan_tram_kiem_tra, diem_kiem_tra, diem_thi1, diem_thi2, hoc_ky
			FROM diem a INNER JOIN mon_hoc b ON a.mmh = b.mmh		
			WHERE mssv = '$student_id'			
			ORDER BY a.mmh, b.ten_mh";

		$result = mysqli_query($link, $sql);
		if(!$result)
		{
			$error = 'Error fetching student to display';
			include '/includes/error.html.php';
			exit();
		}
		$max = 0;
		$scores = array();
		while($row = mysqli_fetch_array($result))
		{
			$test_percentage = $row['phan_tram_kiem_tra'];
			$test_score = $row['diem_kiem_tra'];
			$exam_score1 = $row['diem_thi1'];
			$exam_score2 = $row['diem_thi2'];
			$final1 = $test_percentage / 100 * $test_score + (1 - $test_percentage / 100) * $exam_score1;
			$final2 = $test_percentage / 100 * $test_score + (1 - $test_percentage / 100) * $exam_score2;
			if( $row['hoc_ky'] > $max )
				$max = $row['hoc_ky'];
			$scores[] = array(
						  'subject_id' => $row['mmh'],						  
						  'subject_name' => $row['ten_mh'],					  
						  'test_percentage' => $test_percentage,
						  'test_score' => $test_score,
						  'exam_score1' => $exam_score1,
						  'final1' => $final1,				  
						  'exam_score2' => $exam_score2,
						  'final2' => $final2,
						  'term' => $row['hoc_ky']
			);
		}
		// store scores
		$_SESSION['scores'] = $scores;
		$_SESSION['max'] = $max;
	}
}
?>